package com.system.manager.modules.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.manager.common.enums.IErrorCode;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.authorized.enums.AuthorizationErrorCode;
import com.system.manager.modules.authorized.exception.AuthorizationException;
import com.system.manager.modules.authorized.mapper.UserMapper;
import com.system.manager.modules.authorized.service.IUserService;
import com.system.manager.modules.risk.RiskErrorCode;
import com.system.manager.modules.risk.dto.StaffStationVo;
import com.system.manager.modules.risk.entity.StaffStation;
import com.system.manager.modules.risk.entity.Station;
import com.system.manager.modules.risk.exception.riskException;
import com.system.manager.modules.risk.mapper.StaffStationMapper;
import com.system.manager.modules.risk.mapper.StationMapper;
import com.system.manager.modules.risk.service.IStaffStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-12
 */
@Service
public class StaffStationServiceImpl extends ServiceImpl<StaffStationMapper, StaffStation> implements IStaffStationService {

    @Autowired
    private StaffStationMapper staffStationMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StationMapper stationMapper;


    //查找用户所在部门人员
    @Override
    public StaffStation findStaffStaionById(Integer id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",id);
        StaffStation staffStation = staffStationMapper.selectOne(wrapper);
        if(staffStation == null) throw new riskException(RiskErrorCode.NON_FIND_EXAM_RISK);
        if(staffStation.getSId() == null) throw new riskException(RiskErrorCode.NON_FINDSTATION_EXAM_RISK);
        return staffStation;
    }

    //将某个人加入到哪个部门中
    @Override
    public Integer addStation(StaffStationVo staffStationVo) {
        String uName = staffStationVo.getUName();
        String sName = staffStationVo.getSName();
        String uPhone = staffStationVo.getUPhone();
        //查找用户唯一id
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name",uName);
        User user = userMapper.selectOne(wrapper);
        Integer userId = user.getUserId();
        System.out.println("======================"+userId);
        //查找部门id
        QueryWrapper wrappers = new QueryWrapper();
        wrappers.eq("s_name",sName);
        Station station = stationMapper.selectOne(wrappers);
        Integer sId = station.getSId();

        System.out.println("======================"+sId);

        StaffStation staffStation = new StaffStation();

        //上传用户唯一标识id
        staffStation.setUId(userId);

        //上传用户名字
        staffStation.setUName(uName);

        //上传岗位id
        staffStation.setSId(sId);

        //上传岗位名字
        staffStation.setSName(sName);

        //上传联系人电话
        staffStation.setUPhone(uPhone);

        //注册时间
        Date date = new Date();
        staffStation.setUDate(date);

        System.out.println(staffStation);
        staffStationMapper.insert(staffStation);
        return null;
    }

    //根据部门Id查询出所有的部门成员
    @Override
    public List<StaffStation> listFindByStationId(Integer stationId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("s_id",stationId);
        List<StaffStation> staffStationList = staffStationMapper.selectList(wrapper);
        return staffStationList;
    }
}
